import { list, del, edit, editpwd, batchdel } from '@/api/Account/account.api'
import { formartTime } from "@/utils/timeFormart";
import { Message, MessageBox } from 'element-ui';
import Vue from 'vue'
const vue = new Vue()
export default {
    namespaced: true,
    state() {
        return {
            AccountList: [],
            total: 10,
            currentPage: 1,
            pageSize: 5,
            ids:[]
        }
    },
    getters: {
        //编辑显隐
        change_editAble(state) {
            return state.AccountList.map(item => {
                //时间戳
                item.ctime = formartTime(item.ctime)
                //显隐
                vue.$set(item, 'editAble', false)
                return item
            })
        },

    },
    mutations: {

        update_AccountList(state, payload) {
            state.AccountList = [...payload]
        },
        update_total(state, payload) {
            state.total = payload
        },
        update_currentPage(state, payload) {
            state.currentPage = payload
        },
        update_pageSize(state, payload) {
            state.pageSize = payload
        },
        update_ids(state, payload) {
            state.ids = payload
        },

    },
    actions: {
        //更新列表
        sync_update_AccountList({ state, commit }) {
            let { currentPage, pageSize } = state
            list({ currentPage, pageSize }).then(res => {
                commit('update_AccountList', res.data.data)
                commit('update_total', res.data.total)
            })

        },

        //删除账号
        sync_del_Account({ dispatch }, id) {
            MessageBox.confirm('确认删除吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                del({ id }).then(res => {
                    dispatch('sync_update_AccountList')
                })
                Message({
                    type: 'success',
                    message: '删除成功!'
                })
            }).catch(() => {
                Message({
                    type: 'info',
                    message: '取消删除!'
                })
            })

        },

        //提交编辑
        sync_edit_Account({ dispatch }, data) {
            MessageBox.confirm('确认提交吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                edit(data).then(res => {
                    dispatch('sync_update_AccountList')
                })
                Message({
                    type: 'success',
                    message: '更改成功!'
                })
            }).catch(() => {
                Message({
                    type: 'info',
                    message: '取消更改!'
                })
            })


        },
        //修改密码
        sync_editpwd({ dispatch }, data) {
            MessageBox.confirm('确认提交吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                editpwd(data).then(res => {
                    dispatch('sync_update_AccountList')
                })
                Message({
                    type: 'success',
                    message: '更改成功!'
                })
            }).catch(() => {
                Message({
                    type: 'info',
                    message: '取消更改!'
                })
            })
        },

        //批量删除
        update_deltotal(ids) {
            batchdel({ ids }).then(res => {
                console.log(res);

            })
        }
    },
    modules: {}


}